package com.situ.company.project.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.situ.company.project.dao.IProjectDao;
import com.situ.company.project.model.ProjectModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

public class ProjectDaoImpl implements IProjectDao{
	public Integer insert(ProjectModel model) {
		String sql = "insert into project(code,name,date)values(?,?,?)";
		return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getDate());
	}

	public Integer updateActive(ProjectModel model) {
		StringBuffer sql = new StringBuffer("update project set id=id ");
		List<Object> vals = appendUpdate(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}
	
	/**
	 * 拼接更新内容
	 * @param sql
	 * @param model
	 * @return
	 */
	public List<Object> appendUpdate(StringBuffer sql,ProjectModel model){
		List<Object> vals=new ArrayList<>();
		String name=model.getName();
		if(name!=null) {
			vals.add(name);
			sql.append(",name=?");
		}
		String date=model.getDate();
		if(date!=null) {
			vals.add(date);
			sql.append(",date=?");
		}
		String codeEmp=model.getCodeEmp();
		if(codeEmp!=null) {
			vals.add(codeEmp);
			sql.append(",code_emp=?");
		}
		
	
		sql.append(" where code=?");
		vals.add(model.getCode());
		return vals;
	}

	public Integer updateAll(ProjectModel model) {
		String sql = "update project set name=?,date=? where code=?";
		return JDBCUtil.update(sql, model.getName(), model.getDate(), model.getCode());
	}

	public Integer deleteBycode(ProjectModel model) {
		String sql = "delete from project where code=?";
		return JDBCUtil.update(sql, model.getCode());
	}

	public Integer delete(ProjectModel model) {
		StringBuffer sql = new StringBuffer("delete from project ");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), vals);
	}

	public ProjectModel selectModel(ProjectModel model) {
		StringBuffer sql = new StringBuffer("select *,(select name from employee e where e.code=p.code_emp) name_emp from project p");
		List<Object> vals = appendWhere(sql, model);
		Map<String, String> props = new HashMap<>();
		props.put("code", "code");
		props.put("name", "name");
		props.put("date", "date");
		props.put("codeEmp", "code_emp");
		props.put("nameEmp", "name_emp");
		
		
		return JDBCUtil.queryModel(sql.toString(), vals, ProjectModel.class, props);
	}

	public List<ProjectModel> selectList(ProjectModel model) {
		StringBuffer sql = new StringBuffer("select *,(select name from employee e where e.code=p.code_emp) name_emp from project p");
		List<Object> vals = appendWhere(sql, model);
//		List<String> props=Arrays.asList("code","name","pass");
		Map<String, String> props = new HashMap<>();
		props.put("code", "code");
		props.put("name", "name");
		props.put("date", "date");
		props.put("codeEmp", "code_emp");
		props.put("nameEmp", "name_emp");
	
		
		return JDBCUtil.queryList(sql.toString(), vals, ProjectModel.class, props);
	}

	public Integer selectCount(ProjectModel model) {
		StringBuffer sql=new StringBuffer("select count(1) from project");
		List<Object> vals=appendWhere(sql, model);
		return JDBCUtil.queryInt(sql.toString(), vals);
	}

	public List<Object> appendWhere(StringBuffer sql, ProjectModel model) {
		List<Object> vals = new ArrayList<>();
		sql.append(" where 1=1 ");
		String code = model.getCode();
		if (!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ? ");
			vals.add(code);
		}
		String name = model.getName();
		if (!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ? ");
			vals.add(name);
		}
		String date = model.getDate();
		if (!FmtEmpty.isEmpty(date)) {
			sql.append(" and date like ? ");
			vals.add(date);
		}
		
		if(model.isPageOn()) {
			//1-0,10
			//2-10,10
			//3-20,10
			sql.append(" limit ?,?");
			vals.add(model.getRowStart());
			vals.add(model.getPageLimit());
		}
		return vals;

	}
}
